package cn.tedu.service.impl;

import java.util.List;

import cn.tedu.dao.UserDao;
import cn.tedu.dao.impl.UserDaoImpl;
import cn.tedu.entity.User;
import cn.tedu.service.UserService;
import cn.tedu.vo.Page;
/**
 * 用户业务实现
 * @author Administrator
 *
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDaoImpl();
	@Override
	public boolean login(String uname, String upassword) {
		boolean flag=false;
		try {
			int id=userDao.login(uname,upassword);
			if(id>0) {
				flag=true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public boolean addUser(User user) {
		boolean flag=false;
		try {
			int rowAffect=userDao.addUser(user);
			if(rowAffect==1) {
				flag=true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public List<User> findAllUsers() {
		List<User> users=null;
		try {
			users = userDao.findAllUser();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return users;
	}
	@Override
	public boolean deleteUser(int id) {
		boolean flag=false;
		try {
			int rowAffect=userDao.deleteUser(id);
			if(rowAffect==1) {
				flag=true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public User findUserById(int id) {
		User user=null;
		try {
			user=userDao.findUserById(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	@Override
	public boolean updateUser(User user) {
		boolean flag=false;
		try {
			int rowAffect=userDao.updateUser(user);
			if(rowAffect==1) {
				flag=true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public Page<User> findUserByPage(int currentPage, int pageSize, String[] keywords) {
		Page<User> page=new Page<User>();
		try {
			page.setCurrentPage(currentPage);
			page.setPageSize(pageSize);
			page.setKeywords(keywords);
			
			//查询数据库
			int totalCount=userDao.getCount(keywords);
			page.setTotalCount(totalCount);
			
			int totalPage=(totalCount%pageSize==0)? (totalCount/pageSize) : (totalCount/pageSize)+1;
			page.setTotalPage(totalPage);
			
			if(currentPage==1) {
				page.setPreviousPage(1);
			}else {
				page.setPreviousPage(currentPage-1);
			}
			
			if(currentPage==totalPage) {
				page.setNextPage(totalPage);
			}else {
				page.setNextPage(currentPage+1);
			}
			List<User> users=userDao.getUsersByPage(currentPage,pageSize,keywords);
			page.setData(users);		
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return page;
	}
	@Override
	public boolean findUserByName(String uname) {
		boolean flag=false;
		try {
			int id=userDao.findUserByName(uname);
			if(id>0) {
				flag=true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

}
